using UnityEngine;
using System.Collections;
 
public class EmitRPGWeaponTrail : CharacterAction
{
    public bool gatherDeltaTimeAutomatically = true;
    public float timeToTweenTo = 0.2f;
    public float trailTime = 0.6f;
    public float tweenSpeed = 0.3f;
    public float fadeOutTime = 0.4f;
    public float fadeInTime = 0.4f;

    public float t = 0.033f;
    public float tempT = 0;

    protected float m = 0;
    public float animationIncrement = 0.0003f; // ** This sets the number of time the controller samples the animation for the weapon trails
    //protected float t = 0.033f;
    //private float tempT = 0;

    public bool isShowing = true;

	public override void OnActionEnter (Character character)
	{
        base.OnActionEnter(character);
        //设置拖尾时间
        for(int i=0;i<character.RPGTrails.Length;i++)
        {
            character.RPGTrails[i].StartTrail(timeToTweenTo, fadeInTime);
        }
        
        //设置播放时间
        for(int i=0;i<character.RPGTrails.Length;i++)
        {
            character.RPGTrails[i].SetTime (trailTime, timeToTweenTo, tweenSpeed);
        }

        for(int i=0;i<character.RPGTrails.Length;i++)
        {
            //character.RPGTrails[i].FadeOut(fadeOutTime);
        }

        isShowing = true;
    }
    
    public void SetDeltaTime (float deltaTime)
    {
        t = deltaTime; // ** This is for forcing the deltaTime of the Animation Controller for personal slow motion effects
    }

    /** override to implement*/
    //public override void OnFrameUpdate(Character.FrameUpdate arg)
    public override void OnFrameUpdate(Character.FrameUpdate arg)
    {
        if (gatherDeltaTimeAutomatically)
        {
            //t = Mathf.Clamp (Time.fixedDeltaTime, 0, 0.066f);
        }

        if (t > 0) {

            while (tempT < t) {
                
                //
                // ** This loop runs slowly through the animation at very small increments
                // ** a bit expensive, but necessary to achieve smoother than framerate weapon trails
                //
                tempT += animationIncrement;
                //
                m = tempT / t;
                // ** Adds the information to the WeaponTrail
                //
                for (int i = 0; i < character.RPGTrails.Length; i++) {
                    if (character.RPGTrails[i].time > 0) {
                        character.RPGTrails[i].Itterate (Time.time - t + tempT);
                    } else {
                        character.RPGTrails[i].ClearTrail ();
                    }
                }
            }
            //
            // ** End of loop
            //
            tempT -= t;
            //

            // ** Finally creates the meshes for the WeaponTrails (one per frame)
            //
            for (int i = 0; i < character.RPGTrails.Length; i++)
            {
                if (character.RPGTrails[i].time > 0)
                {
                    character.RPGTrails[i].UpdateTrail (Time.time, t);
                }
            }
        }
    }

    public override void OnActionExit (Character character)
    {
        isShowing = false;
        base.OnActionExit(character);
        for(int i=0;i<character.RPGTrails.Length;i++)
        {
            character.RPGTrails[i].FadeOut(fadeOutTime);
            character.RPGTrails[i].ClearTrail();
        }
    }
}

